﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Messaging;
using System.Text;
using System.Threading.Tasks;
using JSTT_DATA_ENTITY;
using JSTT.IPC;
using TSComm.Utilities.Datas;

namespace JSTT.DataThreadProcess
{
    /// <summary>
    /// 处理设备数据
    /// </summary>
    class THProcessDeviceData
    {
        private DTMessageQueue dtMessage = new DTMessageQueue();
        //private NamedPipeListenServer npListenServer = new NamedPipeListenServer("JSTT_PIPE");
        private int messageCount=1;
        private JSTTDevDataManager jsttDevDataManager;
        //private List<TD_DATATYPE_FORMAT_RULES> td_datatype_format_rules_list;

        public THProcessDeviceData()
        {
            dtMessage.MessageReceived += MessageRecivedHandler;
            jsttDevDataManager = JSTTDevDataManager.Instance();
            //td_datatype_format_rules_list = TD_DATATYPE_FORMAT_RULES.GetAllRules();
        }


        /// <summary>
        /// 线程B，接收消息Queue线程
        /// </summary>
        public void ReceiveMessages()
        {
            //npListenServer.Run();
            while (true)
            {
                dtMessage.ReceiveMessage();
            }
        }


        /// <summary>
        /// 消息处理程序，处理MessageQueue中传递过来的消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void MessageRecivedHandler(object sender, MessageEventArgs e)
        {
            byte[] bMessageBody = e.BinaryMessageBody;
            //string dataStr = Encoding.ASCII.GetString(bMessageBody);
            Console.WriteLine((messageCount++).ToString() + ". Process Datas:++++"+System.DateTime.Now.ToString());

            TD_DATATYPE_FORMAT_RULES match_dataType = jsttDevDataManager.JudgeDataBelongDataType(bMessageBody);
            if (match_dataType != null)
            {
                I_JSTT_DEV_DATA jstt_dev_data = JSTT_DATA_FACTORY.CreateJSTTData(match_dataType, bMessageBody);
                if (jstt_dev_data != null)
                {
                    jsttDevDataManager.Add_JSTTDeviceData(jstt_dev_data);
                    JSTTLogger.Logger.ProcInfoLogger(string.Format("Add DevData To ReProcess List.DevListCount={0};Dev SIM={1};Dev Type={2};Dev Exception Type={3}", jsttDevDataManager.GetDevDataListCount(), jstt_dev_data.JST_DEVICE_SIM, jstt_dev_data.JST_DATATYPE_ID, jstt_dev_data.JST_EXPT_STATUS));
                }
                else
                {
                    JSTTLogger.Logger.ProcWarnLogger("Program Cannot Generate DevData:" + match_dataType.DF_RULE_NAME);
                }
            }
            else
            {
                JSTTLogger.Logger.ProcWarnLogger("Process Device Data is not valid:" + bMessageBody.ToBSDString());
            }
            
        }


        public void DataProcessTimeAction(object o)
        {
            if (jsttDevDataManager != null && jsttDevDataManager.GetDevDataListCount() > 0)
            {
                JSTTLogger.Logger.ProcInfoLogger("Fulsh DevList:" + jsttDevDataManager.GetDevDataListCount().ToString());
                jsttDevDataManager.FlushDevDataList();
            }
        }
 
    }
}
